var app = new Vue({
    el:'#app',
    data:{
        branch : {
            id:'',
            name : '',
            phone : '',
            email : '',
            province : '',
            city : '',
            district : '',
            address : '',
            note : '',
        }
    },

    //初始化函数
    mounted: function () {
        var id = this.obtain('id');
        if(id){
            $.get("/admin/system/getBranch",{id:id,class:'select'},function (res) {
                    if(res.code){
                        app.branch = res.data
                    } else {
                        layer.msg(res.data);
                    }
                });
        }
    },
    methods: {
        obtain:function (val) {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == val){
                    return pair[1];
                }
            }
        },
        say: function (val) {
            if (app.branch.name == '') {
                layer.msg('公司名字不能为空');return false;
            }
            var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
            if (app.branch.phone == '' && myreg.test(app.branch.phone)) {
                layer.msg('公司电话不能为空');return false;
            }
            // var reg = new RegExp("^[a-z0-9]+([._-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$"); //正则表达式
            // if (app.branch.email == '' || reg.test(app.branch.email)) {
            //     layer.msg('公司邮箱不能为空');return false;
            // }
            if (app.branch.province == '') {
                layer.msg('公司区域省不能为空');return false;
            }
            if (app.branch.city == '') {
                layer.msg('公司区域市不能为空');return false;
            }
            if (app.branch.district == '') {
                layer.msg('公司区域县不能为空');return false;
            }
            if (app.branch.address == '') {
                layer.msg('公司区域详情不能为空');return false;
            }
            $.get('/admin/system/addBranch',app.branch,function (res) {
                if (res.code) {
                    layer.msg(res.data);
                    window.location.href="/admin/system/branch";
                } else {
                    layer.msg(res.data);
                }
            })
        }
    },

});
